package com.ruoyi.commodity.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.ContractTemplates;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commodity.mapper.ContractTemplatesMapper;
import com.ruoyi.commodity.service.IContractTemplatesService;

import static com.ruoyi.common.utils.SecurityUtils.getNickname;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 供应商管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-19
 */
@Service
public class ContractTemplatesServiceImpl implements IContractTemplatesService 
{
    @Autowired
    private ContractTemplatesMapper contractTemplatesMapper;

    /**
     * 查询供应商管理
     * 
     * @param id 供应商管理主键
     * @return 供应商管理
     */
    @Override
    public ContractTemplates selectContractTemplatesById(Long id)
    {
        return contractTemplatesMapper.selectContractTemplatesById(id);
    }

    /**
     * 查询供应商管理列表
     * 
     * @param contractTemplates 供应商管理
     * @return 供应商管理
     */
    @Override
    @DataScope(contractTemplatesAlias = "ct")
    public List<ContractTemplates> selectContractTemplatesList(ContractTemplates contractTemplates)
    {
        return contractTemplatesMapper.selectContractTemplatesList(contractTemplates);
    }

    @Override
    public List<TreeSelect> selectTemplatesTreeList(ContractTemplates contractTemplates) {
        List<ContractTemplates> templatesList = SpringUtils.getAopProxy(this).selectContractTemplatesList(contractTemplates);
        return buildTemplatesTreeSelect(templatesList);
    }

    @Override
    public List<ContractTemplates> buildTemplatesTree(List<ContractTemplates> contractTemplatesList) {
        List<ContractTemplates> returnList = new ArrayList<>();
        List<Long> collect = contractTemplatesList.stream().map(ContractTemplates::getId).collect(Collectors.toList());
        for (ContractTemplates contractTemplates:contractTemplatesList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(contractTemplates.getParentId())){
                recursionFn(contractTemplatesList,contractTemplates);
                returnList.add(contractTemplates);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = contractTemplatesList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildTemplatesTreeSelect(List<ContractTemplates> contractTemplatesList) {
        List<ContractTemplates> templatesTree = buildTemplatesTree(contractTemplatesList);
        return templatesTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public int insertContractTemplates(ContractTemplates contractTemplates) {
        return contractTemplatesMapper.insertContractTemplates(contractTemplates);
    }

    @Override
    public int updateContractTemplates(ContractTemplates contractTemplates) {
        contractTemplates.setAuthor(getUsername());
        contractTemplates.setUpdateTime(DateUtils.getNowDate());
        return contractTemplatesMapper.updateContractTemplates(contractTemplates);
    }

    @Override
    public int deleteByIdInt(Long[] ids) {
        return contractTemplatesMapper.deleteByIdInt(ids);
    }

    private void recursionFn(List<ContractTemplates> contractTemplatesList , ContractTemplates contractTemplates){
        // 得到子节点列表
        List<ContractTemplates> childList = getChildList(contractTemplatesList,contractTemplates);
        contractTemplates.setChildrenTemplates(childList);
        for (ContractTemplates newTemplates : childList)
        {
            if (hasChild(childList, newTemplates))
            {
                recursionFn(contractTemplatesList, newTemplates);
            }
        }
    }

    private List<ContractTemplates> getChildList(List<ContractTemplates> contractTemplatesList, ContractTemplates contractTemplates){
        List<ContractTemplates> returnList = new ArrayList<>();
        Iterator<ContractTemplates> iterator = contractTemplatesList.iterator();
        while (iterator.hasNext()){
            ContractTemplates n = (ContractTemplates) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == contractTemplates.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }
    private boolean hasChild(List<ContractTemplates> contractTemplatesList, ContractTemplates contractTemplates)
    {
        return getChildList(contractTemplatesList, contractTemplates).size() > 0;

    }
}
